<template>
  <div>
    <el-menu
        class="el-menu-demo"
        mode="horizontal"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
      <el-menu-item index="1">{{ nickName }}</el-menu-item>
      <el-menu-item index="1">
        <a href="#/">继续购物</a>
      </el-menu-item>
      <el-menu-item index="1">
        <a href="javascript:;" @click="logout">注销</a>
      </el-menu-item>
    </el-menu>
    <el-table
        :data="goods"
        style="width: 100%">
      <el-table-column
          prop="goodsCode"
          label="编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsName"
          label="名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="goodsPrice"
          label="价格">
      </el-table-column>
      <el-table-column
          prop="provider.data.proName"
          label="供应商">
      </el-table-column>
      <el-table-column
          label="数量"
          width="100">
        <template slot-scope="scope">
          <el-button type="text" size="big" @click="changeNum(scope.row,-1)">-</el-button>
          {{ scope.row.cartNum }}
          <el-button type="text" size="middle" @click="changeNum(scope.row,1)">+</el-button>
        </template>
      </el-table-column>
      <el-table-column
          prop="total"
          label="价格">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="removeGoods(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div align="right">
      <label style="font-size: 4em;">总价：¥<b>{{ total }}</b>元</label>
      <el-button type="primary" plain @click="toOrder">去付款</el-button>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "ShowCart",
  data() {
    return {
      goods: [],
      total: 0,
      nickName:window.localStorage.getItem('nickName')
    }
  },
  methods: {
    loadMyCart() {
      let _this = this;
      axios.get('/api/smbms-goods/goods/show', {
        headers: {
          token: window.localStorage.getItem('token')
        }
      }).then((response) => {
        _this.goods = response.data.data
        _this.total = response.data.errorMsg
      }).catch(() => {
        _this.$router.push('login')
      })
    },
    removeGoods(gid) {
      this.$confirm('你确定要删除么, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let _this = this
        axios.get('/api/smbms-goods/goods/remove', {
          headers: {
            token: window.localStorage.getItem('token')
          },
          params: {
            goodsId: gid
          }
        }).then((response) => {
          _this.goods = response.data.data
          _this.total = response.data.errorMsg
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    changeNum(row, change) {
      if (row.cartNum + change < 0) {
        this.$message({
          type: 'error',
          message: '数量不能小于0！'
        });
        return;
      }
      if (row.cartNum - 0 + change === 0) {
        this.removeGoods(row.id)
        return;
      }
      let _this = this
      axios.get('/api/smbms-goods/goods/update/' + row.id + '/' + (row.cartNum - 0 + change), {
        headers: {
          token: window.localStorage.getItem('token')
        }
      }).then((response) => {
        _this.goods = response.data.data
        _this.total = response.data.errorMsg
        this.$message({
          type: 'success',
          message: '更新成功!'
        });
      })
    },
    toOrder(){
      this.$router.push('order')
    }
  },
  created() {
    this.loadMyCart()
  }
}
</script>

<style scoped>

</style>